// 版本号 v1.2.6
const HOST = require('./config.js').host

const P = require('./utils/getParam.js').getParam
App({
    globalData:{
        isGuide: null, // 用户是否是导游
        userInfo: null, // 用户信息
        order: {}, // 订单信息
        postage: null, // 邮费
        ceil: 99, // 
        cartNum: 0, //购物车
        // 带参数二维码参数
        // store: null,
        // location: null,
        // address: null
        qrcode: {} ,// 二维码参数,
        
    },

    userId: '', // openid
    codeId:'',

    /**
     * 验证用户身份
     */
    verify (id) {
        let self = this

        wx.request({
            url: HOST + '' + id,
            method: 'GET',
            success (res) {
                if (res.data.sysCode === 1) {
                    self.globalData.isGuide = true
                }
            }
        })
    },

    getUserInfo(cb) {
        let self = this
        
        
        wx.login({
            success(res) {
                if (res.code) {
                    wx.request({
                        url: HOST + 'v1/login/' + res.code,
                        method: 'GET',
                        success:res=>{
                            if (res.data.sysCode === 1) {
                                self.userId = res.data.data
                                // self.verify(self.userId)
                                console.log("初始化app,app.js,uerId:" + self.userId);
                            }
                        },
                        complete:()=>{
                          
                          wx.request({
                            url: `${HOST}v1/userInfo/getListById/${self.userId}`,
                            success: res => {
                              self.codeId = res.data.data.codeId || ""
                            }
                          })
                        }
                    })
                } else {
                    console.log('获取用户登录态失败！' + res.errMsg)
                }

                wx.getUserInfo({
                    success(res) {
                      
                        self.globalData.userInfo = res.userInfo
                    }
                })
            }
        })
    },
    // getCartNum() {
    //   let self = this
    //   wx.getStorage({
    //     key: 'cart',
    //     success: function (res) {
    //       if (res.data) {
    //         self.globalData.cartNum = res.data.length
    //       }
    //     }
    //   })
    // }
    // ,
    
    onLaunch(options) {
        let self = this
        // options.query.q = 'index?companyId=ggyl&companyKey=70'
        

        if(options.query.q){
          let companyId = P('companyId', options.query.q)
          console.log(companyId)
          let companyKey = P('companyKey', options.query.q)
          console.log(companyKey)
          wx.login({
            success(res) {
              wx.request({
                url: HOST + 'v1/login/' + res.code,
                method: 'GET',
                success(res) {
                  let userId = res.data.data
                  wx.request({
                    url: `${HOST}v1/qrcode/findByCompanyIdAndCompanyKey/${companyId}/${companyKey}`,
                    success: res => {
                      console.log(self.globalData)
                      console.log(res)
                      let codeId = res.data.data.id
                      self.codeId = codeId
                      console.log('codeId'+codeId)
                      wx.request({
                        url: `${HOST}v1/scanrecord/saveScanRecord`,
                        data: {
                          codeId: codeId,
                          recordType: 'A',
                          userId: userId
                        },
                        method: 'POST',
                        success: res => {
                          console.log(res)
                          console.log(codeId)
                          console.log(self.userId)
                        }
                    })
                  }
                })
            }})   
          }})
        } 
        console.log(`Use:${wx.canIUse('reLaunch')}`)
        console.log(`rich:${wx.canIUse('rich-text')}`)
        if (!wx.canIUse('reLaunch')) {
          wx.showToast({
            title: '微信版本过低，请更新后使用',
            icon: 'loading',
            duration: 2000
          })
          return
        }
        wx.getSystemInfo({
          success: function (res) {
            console.log(`SDKcanUse:${res.SDKVersion !== null}`)
            console.log(`SDK:${res.SDKVersion}`)
            if (res.SDKVersion == null ){
              wx.showToast({
                title: '微信版本过低，请更新后使用',
                icon: 'loading',
                duration: 2000
              })
              return
            }
                
          }
        })

        // getCartNum()

        wx.request({
            url: HOST + 'v1/postage/queryPostage/0',
            method: 'GET',
            success(res) {
                if (res.data.sysCode === 1) {
                    self.globalData.ceil = res.data.data
                }
            }
        })

        this.getUserInfo()
    }
})